#include <bits/stdc++.h>
#define int long long 
using namespace std;

const int maxn = 1e5 + 5;
int dp[maxn][2];

// 链式前向星
struct Edge{
	int to, nxt;
}edges[maxn<<1];

int head[maxn], cnt;

void add(int from , int to){
	edges[++cnt].nxt = head[from];
	edges[cnt].to = to;
	head[from] = cnt;
	return ;
}

int n, hap[maxn];



void dfs(int c, int p){
	
	for (int i=head[c];i;i=edges[i].nxt) {
		int to = edges[i].to;
		if (to == p) {
			continue;
		}
		dfs(to, c);
		dp[c][0] += max(dp[to][1], dp[to][0]);
		dp[c][1] += dp[to][0];
	}
}

void solve(){
	cin >> n;
	for (int i=1;i<=n;i++) {
		cin >> hap[i];
		dp[i][1] = hap[i];
	}
	
	for (int i=1;i<n;i++) {
		int a, b;
		cin >> a >> b;
		add(a, b);
		add(b, a);
	}
	
	dfs(1, 0);
	
	int ans = max(dp[1][0], dp[1][1]);
	cout << ans << '\n';
	
}

signed main(){
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	solve();
	return 0;
}

